/**
 * 
 */
package com.zc.BankCreditManagement.ServiceImpl;

import java.util.ArrayList;

import com.zc.BankCreditManagement.DAO.ManagerDAO;
import com.zc.BankCreditManagement.JavaBean.Manager;
import com.zc.BankCreditManagement.Service.ManagerService;

/**
 * @author ZhangCheng
 * @time 2019年9月17日
 */
public class ManagerServiceImpl implements ManagerService {
	ManagerDAO manDAO;

	public ManagerServiceImpl() {
		this.manDAO = new ManagerDAO();
	}

	@Override
	public int insertNewToDB(Manager man) {
		return this.manDAO.insertNewMan(man);
	}

	/**
	 * 删除该经理
	 * 
	 * @return 1：已经被删除 0：删除失败
	 */
	@Override
	public int deleteThisToDB(int manId,boolean excludeDelete) {
		if (this.manDAO.getManagerInfoById(manId, excludeDelete) != null) {
			return this.manDAO.deleteManById(manId);
		} else {
			System.out.println("删除失败！您的经理信息有误，不存在该用户或已经被删除，请检查您的信息！");
		}
		return 0;
	}

	/**
	 * 获取该经理信息
	 * 
	 * @return
	 * 
	 *         null：没有该经理信息
	 */
	@Override
	public Manager getThisInfoById(int manId,boolean excludeDelete) {
		return this.manDAO.getManagerInfoById(manId, excludeDelete);
	}

	/**
	 * 获取所有经理的信息
	 * 
	 * @return 所有经理的集合
	 * 
	 */
	@Override
	public ArrayList<Manager> getAllSuitable(boolean excludeDelete) {
		StringBuffer sql = new StringBuffer("select * from Manager ");
		if (excludeDelete)
			sql.append("isdelete = 1");
		return this.manDAO.getManagersBySQLFromDB(sql.toString());
	}

	/**
	 * 修改该经理信息
	 * 
	 * @return 1：已经被修改 0：修改失败
	 */
	public int updateManager(Manager man,boolean excludeDelete) {
		if (this.manDAO.getManagerInfoById(man.getManId(), true) != null) {
			return this.manDAO.updateManInfo(man,excludeDelete);
		} else {
			System.out.println("更新失败！您的经理信息有误，不存在该经理，请检查您的信息！");
		}
		return 0;
	}
	
	/**
	 * 随机选择经理进行分配
	 * 
	 * @return 被选择的经理信息
	 */
	public Manager ranGetOneMan() {
		int ran = (int) (Math.random()*Math.random()*10000);
		ArrayList<Manager> man = this.manDAO.getManagersInfo(true);
		int whichMan = ran % man.size();

		return man.get(whichMan);
	}
	
	/**
	 * 注册验证
	 *
	 */
	public String regiVerify(int manId, String email) {
		Manager m = this.manDAO.getManagerInfoById(manId, true);
		if(m != null) {
			if(email.equals(m.getEmail())) {
				return "Success 正在向您的邮箱发送验证码，请稍候";
			}else {
				return "您的邮箱输入的不正确！";
			}
		}else {
			return null;
		}
	}

}
